package cn.tedu.yy;

import javax.swing.*;

public class Card extends JButton {

    public Card(String name){
        setName(name);
        ImageIcon icon = new ImageIcon("images/"+name+".png");
        setIcon(icon);
        setDisabledIcon(new ImageIcon("images/"+name+"2.png"));
        setSize(icon.getIconWidth(), icon.getIconHeight());
        setContentAreaFilled(false);
    }

    /**
     * 当前牌盖住了 另外一张牌
     * @param other 被盖住的牌
     * @return 盖住了，返回true
     */
    public boolean covered(Card other){
        int x1 = other.getX() - 59;
        int x2 = other.getX() + 59;
        int y1 = other.getY() - 66;
        int y2 = other.getY() + 66;
        return  (getX() > x1 && getX() < x2) &&
                (getY() > y1 && getY() < y2);
    }

    @Override
    public String toString() {
        return getName();
    }

    public boolean sameAs(Card card){
        String name = getName();
        if (name == null){
            return false;
        }
        return name.equals(card.getName());
    }
}
